package com.put.mybills.activities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.put.mybills.R;
import com.put.mybills.adapter.BillListAdapter;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.adapter.TabSwipeFacture;
import com.put.mybills.model.Bill;
import com.put.mybills.model.Facture;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FactureActivity extends FragmentActivity implements TabListener {
	
	private static final String TAG = "FactureActivity";
	private ListView myBillList;
	private BillListAdapter myBillListAdapter;	
	private static int START_BILL = 111;	
	private Facture facture;
	private ViewPager viewPager;
    private TabSwipeFacture mAdapter;
    private ActionBar actionBar; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_facture);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
	    getActionBar().setHomeButtonEnabled(true);	   

	    String factureName="";	    
	    
	    Log.d(TAG,getIntent().getStringExtra("FactureName"));	
		if(getIntent().getStringExtra("FactureName") != null)			
			factureName = getIntent().getStringExtra("FactureName") ;
		else
			return;			
	
		facture = GlobalParams.getInstance().facturedatasource.getFacture(factureName);

		viewPager = (ViewPager) findViewById(R.id.swapableview);
        actionBar = getActionBar();

        mAdapter = new TabSwipeFacture(getSupportFragmentManager(),factureName);
        viewPager.setAdapter(mAdapter);
        
       
        actionBar.setHomeButtonEnabled(true);
        actionBar.setTitle(R.string.back);        
        actionBar.show();
 
		
		if (facture != null){	
			
			
			myBillList = (ListView) findViewById(R.id.list_mybills); 	
			
			List<String> billName = new ArrayList<String>();
			
			List<Bill > bills = GlobalParams.getInstance().billdatasource.getAllBills(facture.getId());
			Log.d(TAG, "Bills = " + bills.size());
			
			if (bills.size() > 0) {			
			
				for (int i=0;  i < bills.size() ; i++){
					Log.d(TAG, "Bills name = " + bills.get(i).getName());
					billName.add(bills.get(i).getName());
				}
				
				String[] values =  billName.toArray(new String[billName.size()]);
				myBillListAdapter = new BillListAdapter(this, values,bills,getResources().getString(R.string.bill_paid),getResources().getString(R.string.bill_notpaid) );
				myBillList.setAdapter(myBillListAdapter);	
				
				final Intent intent = new Intent(FactureActivity.this, BillActivity.class);
				myBillList.setOnItemClickListener(new OnItemClickListener() {
	
					@Override
					public void onItemClick(AdapterView<?> parent, View view,int position, long id) {		
						String item = (String) (myBillList.getItemAtPosition(position));				
						intent.putExtra("BillName", item);
						startActivityForResult(intent, START_BILL);
					}});			
		
			}			
			else {				
				
				TextView emptyBillList = new TextView(this);
		  		emptyBillList.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
		  		emptyBillList.setText(R.string.facture_bills_data_not_exsist);
		  		LinearLayout layout = (LinearLayout) findViewById(R.id.facture_layout);
				layout.addView(emptyBillList);	
				myBillList.setVisibility(LinearLayout.GONE);				
		
			}		
		}			
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d(TAG, "Reqest is " + requestCode + " code is "+ resultCode);
	    if (requestCode == START_BILL) {
	        
	        if (resultCode == 0) {
	        	generateBillList(false);       	
	        	
	        }
	    }
	}
	
	public void generateBillList(Boolean ifSort)  {
		List<String> billName = new ArrayList<String>();
    	
    	List<Bill > bills = GlobalParams.getInstance().billdatasource.getAllBills(facture.getId());
		Log.d(TAG, "Bills = " + bills.size());
		
		if (ifSort)
			Collections.sort(bills);
		
		for (int i=0;  i < bills.size() ; i++){
			Log.d(TAG, "Bills name = " + bills.get(i).getName());
			billName.add(bills.get(i).getName());
		}
		
		String[] values =  billName.toArray(new String[billName.size()]);
		myBillListAdapter = new BillListAdapter(this, values,bills,getResources().getString(R.string.bill_paid),getResources().getString(R.string.bill_notpaid) );

		myBillList.setAdapter(myBillListAdapter);	
		
	}

	public void removeFacture() {
		
		GlobalParams.getInstance().facturedatasource.deleteFacture(facture);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {				
		getMenuInflater().inflate(R.menu.main_facture_activity, menu);    	
        return true;
	}
	


	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		
	    switch (menuItem.getItemId()) {
	    case android.R.id.home:	      
	      super.onBackPressed();
	      break;
	    case R.id.remove_current_facture:
	    	removeFacture();
	    	super.onBackPressed();
	    	break;
	    case R.id.sort_bill_list:
	    	generateBillList(true);	    	
	    	break;	
	    
	    }
	    
	  return (super.onOptionsItemSelected(menuItem));
	}	

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {}
	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {}
}
